package menupopupplugin2.popup.actions;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class InputTemplateFilePage extends CustomWizardPage {
	private Composite container;
	private Text tfModelTemplatePlace;
	private boolean isHave;
	private String modelTemplatePlace;

	public InputTemplateFilePage() {
		super("InputTemplateFilePage");
		setTitle("Input template file page");
		setDescription("Page for let user open file dialog and "
				+ "choose their file they want.");
	}

	@Override
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 3;

		Label lblConnectionURL = new Label(container, SWT.NULL);
		lblConnectionURL.setText("Put model template file");

		tfModelTemplatePlace = new Text(container, SWT.BORDER | SWT.SINGLE);
		tfModelTemplatePlace.setText("");
		tfModelTemplatePlace.setEditable(false);
		tfModelTemplatePlace.addModifyListener(new ModifyListenerImpl());

		Button btnBrowser = new Button(container, SWT.PUSH);
		btnBrowser.setText("Browser");
		btnBrowser.addSelectionListener(new BrowserSelectionListener());

		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		tfModelTemplatePlace.setLayoutData(gd);
		// Required to avoid an error in the system
		setControl(container);
		setPageComplete(false);

	}

	private class BrowserSelectionListener implements SelectionListener {
		@Override
		public void widgetSelected(SelectionEvent arg0) {
			FileDialog fd = new FileDialog(getShell(), SWT.OPEN);
			fd.setText("Open");
			String[] filterExt = { "*.xls" };
			fd.setFilterExtensions(filterExt);
			tfModelTemplatePlace.setText(fd.open());
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent arg0) {

		}
	}

	private class ModifyListenerImpl implements ModifyListener {

		@Override
		public void modifyText(ModifyEvent arg0) {
			String txtTemplatePlace = tfModelTemplatePlace.getText();
			if (!txtTemplatePlace.isEmpty()
					&& txtTemplatePlace.lastIndexOf('.') != -1) {
				String extensionOfFile = txtTemplatePlace.substring(
						txtTemplatePlace.lastIndexOf('.') + 1,
						txtTemplatePlace.length());

				if ("xls".equals(extensionOfFile))
					setPageComplete(true);
			} else
				setPageComplete(false);
		}
	}

	@Override
	public boolean isFinish() {
		return false;
	}

	public IWizardPage getNextPage() {
		// if (rdAutomatic.getSelection())
		// return getWizard().getPage("Three");
		modelTemplatePlace = tfModelTemplatePlace.getText();
		isHave = true;
		return super.getNextPage();
	}

	public boolean isHave() {
		return isHave;
	}

	public void setHave(boolean isHave) {
		this.isHave = isHave;
	}

	public String getModelTemplatePlace() {
		return modelTemplatePlace;
	}
}
